/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SessionBeans.logs;

import SessionBeans.actions.OperationTypeEnum;
import entities.*;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

/**
 *
 * @author Tomas
 */
@Stateless
@LocalBean
public class FolderChangeLogService {

    @EJB
    private OperatioTypeSessionBean operatioTypeSessionBean;
    @PersistenceContext(unitName = "tCloudEE-ejbPU")
    private EntityManager em;

    public void addLogRow(Long userId, Folder folder, OperationTypeEnum operationType) {
        OperationType loadOperationType = operatioTypeSessionBean.load(operationType);

        FolderChangeLog log = new FolderChangeLog(new Date(), folder.getName(), folder.getId(), loadOperationType, userId);

        em.persist(log);
    }

    public List<FolderChangeLog> loadAllChanges(Folder folder) {
        TypedQuery<FolderChangeLog> createNamedQuery = em.createQuery("Select OBJECT(u) from FolderChangeLog u where u.folderId = '" + folder.getId() + "' order by changeDate desc", FolderChangeLog.class);
        List<FolderChangeLog> resultList = createNamedQuery.getResultList();

        return resultList;
    }

    public FolderChangeLog loadLastChange(Folder folder) {
        return loadAllChanges(folder).get(0);
    }
    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
